package com.igroupes.ovineherd.model.attribute.text;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.dto.response.Meta;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.NumberStyle;
import com.igroupes.ovineherd.model.attribute.BasicAttribute;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.enums.FormKey;
import com.igroupes.ovineherd.model.enums.MySqlTypeKey;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class TextBasicAttribute extends BasicAttribute {

    public static final Integer MAXNUMBER = 1000;
    public static final Integer MINNUMBER = 0;
    public static final String MINLABEL = "最小长度";
    public static final String MAXLABEL = "最大长度";
    protected NumberStyle minLength;
    protected NumberStyle maxLength;

    protected TextBasicAttribute() {
        super();
        this.minLength = new NumberStyle(MINLABEL, 0, false, MINNUMBER);
        this.maxLength = new NumberStyle(MAXLABEL, 100, false, MINNUMBER, MAXNUMBER);
    }

    public NumberStyle getMinLength() {
        return minLength;
    }

    public NumberStyle getMaxLength() {
        return maxLength;
    }

    public Boolean verifyField(TextBasicAttribute textBasicAttribute) {
        super.verifyField(textBasicAttribute);
        new NumberStyle().verify(textBasicAttribute.getMinLength());
        new NumberStyle().verify(textBasicAttribute.getMaxLength());
        return true;
    }

    public Boolean verifyFieldV2(TextBasicAttribute basic) {
        super.verifyFieldV2(basic);
        new NumberStyle().verify(new NumberStyle(MINLABEL, basic.getMinLength().getValue(), false, MINNUMBER));
        new NumberStyle().verify(new NumberStyle(MAXLABEL, basic.getMaxLength().getValue(), false, MINNUMBER, MAXNUMBER));
        return true;
    }
    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        TextBasicAttribute textBasicAttribute = (TextBasicAttribute) parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData,dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        fieldQuery.setLength(Convert.toInt(textBasicAttribute.getMaxLength().getValue()));

        DataModelAttributeEntity dataModelAttributeEntity = fieldQuery.getDataModelAttributeEntity();

        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        jsonObject.set("minLength", Convert.toInt( textBasicAttribute.getMinLength().getValue()));
        jsonObject.set("maxLength", Convert.toInt( textBasicAttribute.getMaxLength().getValue()));
        dataModelAttributeEntity.setJson(jsonObject);
        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
        fieldQueries.set(0,fieldQuery);
        return fieldQueries;
    }
    @Override
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        JSONObject jsonObject = super.getAttribute(modelDataJsonAttributeQuery);
        jsonObject.set("minLength", new NumberStyle(MINLABEL, modelDataJsonAttributeQuery.getJson().get("minLength"), false, MINNUMBER));
        jsonObject.set("maxLength", new NumberStyle(MAXLABEL, modelDataJsonAttributeQuery.getJson().get("maxLength"), false, MINNUMBER, MAXNUMBER));

        return jsonObject;
    }

    @Override
    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        List<FieldApiResponse> fieldApiResponses = super.verifyRequestData(modelAttributeEntity, value);
        if(value == null){
            return fieldApiResponses;
        }
        if(!(value instanceof String)){
            throw new ModelException(String.format("%s值类型错误",modelAttributeEntity.getName()));
        }
        String data = (String) value;
        JSONObject jsonObject = modelAttributeEntity.getJson();


        if(ObjectUtil.isNotNull(jsonObject.get("minLength"))) {
            Integer minLength = Convert.toInt(jsonObject.get("minLength"));
            if (data.length() < minLength) {
                throw new ModelException(String.format("%s值长度不能小于%s错误", modelAttributeEntity.getName(), minLength));
            }
        }
        if(ObjectUtil.isNotNull(jsonObject.get("maxLength"))) {
            Integer maxLength = Convert.toInt(jsonObject.get("maxLength"));
            if (data.length() > maxLength) {
                throw new ModelException(String.format("%s值长度不能大于%s错误", modelAttributeEntity.getName(), maxLength));
            }
        }
        FieldApiResponse fieldApiResponse = new FieldApiResponse();
        fieldApiResponse.setDbName(modelAttributeEntity.getDbName());
        fieldApiResponse.setValue(value);
        fieldApiResponse.setIsStr(true);
        fieldApiResponse.setType(MySqlTypeKey.VARCHAR.getCode());
        fieldApiResponses.add(fieldApiResponse);

        return fieldApiResponses;
    }
    @Override
    public Meta getMeta(ModelDataJsonAttributeQuery dataModelAttributeEntity){
        return new Meta(FormKey.TEXT.getCode(), true, true, true);
    }
}
